package com.peng.panels;

import com.peng.component.MyJTextArea;
import com.peng.data.WakCommonData;
import com.peng.event.CzAndCyKeyListener;
import com.peng.event.WakEadBtnsActionListener;
import com.peng.utils.EncryptUtils;
import com.peng.utils.FrameUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author : pengyf
 * @version : 1.0
 * @createTime : 2022/1/23 10:37
 * @Description : TODO
 */
public class WakParmsEadPanel extends JPanel {

    public WakParmsEadPanel() {
        setLayout(null);
        load();
    }

    public WakParmsEadPanel(String panelName) {
        setName(panelName);
        setLayout(null);
        load();
    }

    public WakParmsEadPanel(String panelName, LayoutManager mgr) {
        setName(panelName);
        setLayout(null);
        load();
    }

    // 加载页面元素
    public void load(){
        // 秘钥
        JLabel myLabel = new JLabel();
        myLabel.setText("秘钥：");
        myLabel.setBounds(50,50,70,30);
        myLabel.setFont(WakCommonData.DEFAULTFONT);
        JTextField myTextField = new JTextField();
        myTextField.setBounds(120,50,250,30);
        myTextField.setFont(WakCommonData.DEFAULTFONT);
        myTextField.setText(EncryptUtils.AES_KEY);
        myTextField.getDocument().addUndoableEditListener(WakCommonData.UNDOMANAGER);
        myTextField.addKeyListener(new CzAndCyKeyListener());

        // 偏移量
        JLabel pylLabel = new JLabel();
        pylLabel.setText("偏移量：");
        pylLabel.setBounds(400,50,90,30);
        pylLabel.setFont(WakCommonData.DEFAULTFONT);
        JTextField pylTextField = new JTextField();
        pylTextField.setBounds(500,50,250,30);
        pylTextField.setFont(new Font (Font.DIALOG, Font.BOLD, 16));
        pylTextField.setText(EncryptUtils.IV);
        pylTextField.getDocument().addUndoableEditListener(WakCommonData.UNDOMANAGER);
        pylTextField.addKeyListener(new CzAndCyKeyListener());

        // 操作按钮
        JButton restp = new JButton();
        restp.setBounds(800,50,130,30);
        restp.setText("重置默认密钥");
        restp.setActionCommand("rest");
        restp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton jButton =  (JButton)e.getSource();
                JPanel panel = (JPanel)jButton.getParent();
                JTextField key = (JTextField)panel.getComponent(1);
                JTextField iv = (JTextField)panel.getComponent(3);
                key.setText(WakCommonData.AES_KEY);
                iv.setText(WakCommonData.IV);
            }
        });

        JButton batchCreateJm = new JButton();
        batchCreateJm.setBounds(950,50,120,30);
        batchCreateJm.setText("批量加密");
        batchCreateJm.setActionCommand("batchCz");
        batchCreateJm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton jButton =  (JButton)e.getSource();
                JPanel panel = (JPanel)jButton.getParent();
                JTextField key = (JTextField)panel.getComponent(1);
                JTextField iv = (JTextField)panel.getComponent(3);

                JDialog WakBatchEadDiglog = new JDialog();
                WakBatchEadDiglog.setTitle("批量加密");
                WakBatchEadDiglog.setBounds(10,10,800,460);
                FrameUtils.setDialogCenter(WakBatchEadDiglog);
                WakBatchEadDiglog.setResizable(false);
                // 弹窗默认带了关闭，不需要写
                // setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                FileDialog fileDialog = new FileDialog(WakBatchEadDiglog, "保存生成文件", FileDialog.SAVE);
                WakBatchEadDiglog.add(new WakBatchEadPanel(key.getText(), iv.getText(), fileDialog));
                WakBatchEadDiglog.setVisible(true);
            }
        });

        JLabel pTipLabel = new JLabel();
        pTipLabel.setText("输入参数：");
        pTipLabel.setBounds(50,100, 150,30);
        pTipLabel.setFont(WakCommonData.DEFAULTFONT);
        JScrollPane pjscrollPane = new MyJTextArea(50, 132, 1100, 200).scrollJTextArea();
        JTextArea jam = (JTextArea)pjscrollPane.getViewport().getComponent(0);

        JLabel rTipLabel = new JLabel();
        rTipLabel.setText("处理结果：");
        rTipLabel.setBounds(50,400, 150,30);
        rTipLabel.setFont(WakCommonData.DEFAULTFONT);
        JScrollPane rjscrollPane = new MyJTextArea(50,432, 1100,200).scrollJTextArea();
        JTextArea jem = (JTextArea)rjscrollPane.getViewport().getComponent(0);

        JButton jiamibtn = new JButton();
        jiamibtn.setBounds(460,370,60,30);
        jiamibtn.setText("加密");
        jiamibtn.setActionCommand("jiamibtn");
        jiamibtn.addActionListener(new WakEadBtnsActionListener(jam, jem));

        JButton jiemibtn = new JButton();
        jiemibtn.setBounds(660,370,60,30);
        jiemibtn.setText("解密");
        jiemibtn.setActionCommand("jiemibtn");
        jiemibtn.addActionListener(new WakEadBtnsActionListener(jam, jem));

        this.add(myLabel);
        this.add(myTextField);
        this.add(pylLabel);
        this.add(pylTextField);
        this.add(restp);
        this.add(batchCreateJm);
        this.add(pTipLabel);
        this.add(pjscrollPane);
        this.add(rTipLabel);
        this.add(rjscrollPane);
        this.add(jiamibtn);
        this.add(jiemibtn);
    }


}
